Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / data / src / commonMain / kotlin / org / meshtastic / core / data / repository / RadioConfigRepositoryImpl.kt

Displaying Raw • Download

core/data/src/commonMain/kotlin/org/meshtastic/core/data/repository/RadioConfigRepositoryImpl.kt b4bedd92fcc8d65daff79200d71f6ebc3d8f0cfa (b4bedd92) Text, 8.60 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.repository

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.MutableStateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.asStateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.combine
Tff7b72import T7ee787kotlinx.coroutines.flow.update
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.data.datasource.SwitchingChannelSetDataSource
Tff7b72import T7ee787org.meshtastic.core.datastore.LocalConfigDataSource
Tff7b72import T7ee787org.meshtastic.core.datastore.ModuleConfigDataSource
Tff7b72import T7ee787org.meshtastic.core.model.util.getChannelUrl
Tff7b72import T7ee787org.meshtastic.core.repository.NodeRepository
Tff7b72import T7ee787org.meshtastic.core.repository.RadioConfigRepository
Tff7b72import T7ee787org.meshtastic.proto.Channel
Tff7b72import T7ee787org.meshtastic.proto.ChannelSet
Tff7b72import T7ee787org.meshtastic.proto.ChannelSettings
Tff7b72import T7ee787org.meshtastic.proto.Config
Tff7b72import T7ee787org.meshtastic.proto.DeviceProfile
Tff7b72import T7ee787org.meshtastic.proto.DeviceUIConfig
Tff7b72import T7ee787org.meshtastic.proto.FileInfo
Tff7b72import T7ee787org.meshtastic.proto.LoRaRegionPresetMap
Tff7b72import T7ee787org.meshtastic.proto.LocalConfig
Tff7b72import T7ee787org.meshtastic.proto.LocalModuleConfig
Tff7b72import T7ee787org.meshtastic.proto.ModuleConfig

T8b949e/**
* Class responsible for radio configuration data. Combines access to [nodeDB], [ChannelSet], [LocalConfig] &
* [LocalModuleConfig].
*/
Tff7b72private Tff7b72const Tff7b72val Te6edf3MAX_FILE_MANIFEST_ENTRIES Tff7b72= T79c0ff4T79c0ff0T79c0ff9T79c0ff6

Tf0883e@Single
Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffTooManyFunctionsTa5d6ff"Tb4b4b4) T8b949e// All functions mandated by RadioConfigRepository interface; logically grouped by concern
Tff7b72open Tff7b72class T56d364RadioConfigRepositoryImplTb4b4b4(
Tff7b72private Tff7b72val Te6edf3nodeDBTb4b4b4: Te6edf3NodeRepositoryTb4b4b4,
Tff7b72private Tff7b72val Te6edf3channelSetDataSourceTb4b4b4: Te6edf3SwitchingChannelSetDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3localConfigDataSourceTb4b4b4: Te6edf3LocalConfigDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3moduleConfigDataSourceTb4b4b4: Te6edf3ModuleConfigDataSourceTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3RadioConfigRepository Tb4b4b4{

T8b949e/** Flow representing the [ChannelSet] data store. */
Tff7b72override Tff7b72val Te6edf3channelSetFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3ChannelSetTff7b72> Tff7b72= Te6edf3channelSetDataSourceTb4b4b4.Te6edf3channelSetFlow

T8b949e/** Clears the [ChannelSet] data in the data store. */
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffclearChannelSetTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3channelSetDataSourceTb4b4b4.Te6edf3clearChannelSetTb4b4b4(Tb4b4b4)
Tb4b4b4}

T8b949e/** Replaces the [ChannelSettings] list with a new [settingsList]. */
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffreplaceAllSettingsTb4b4b4(Te6edf3settingsListTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4) Tb4b4b4{
Te6edf3channelSetDataSourceTb4b4b4.Te6edf3replaceAllSettingsTb4b4b4(Te6edf3settingsListTb4b4b4)
Tb4b4b4}

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffupdateChannelSetTb4b4b4(Te6edf3settingsListTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tff7b72?Tb4b4b4, Te6edf3loraConfigTb4b4b4: Te6edf3ConfigTb4b4b4.Te6edf3LoRaConfigTff7b72?Tb4b4b4) Tb4b4b4{
Te6edf3channelSetDataSourceTb4b4b4.Te6edf3updateChannelSetTb4b4b4(Te6edf3settingsListTb4b4b4, Te6edf3loraConfigTb4b4b4)
Tb4b4b4}

T8b949e/**
* Updates the [ChannelSettings] list with the provided channel and returns the index of the admin channel after the
* update (if not found, returns 0).
*
* @param channel The [Channel] provided.
* @return the index of the admin channel after the update (if not found, returns 0).
*/
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffupdateChannelSettingsTb4b4b4(Te6edf3channelTb4b4b4: Te6edf3ChannelTb4b4b4) Tff7b72= Te6edf3channelSetDataSourceTb4b4b4.Te6edf3updateChannelSettingsTb4b4b4(Te6edf3channelTb4b4b4)

T8b949e/** Flow representing the [LocalConfig] data store. */
Tff7b72override Tff7b72val Te6edf3localConfigFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3LocalConfigTff7b72> Tff7b72= Te6edf3localConfigDataSourceTb4b4b4.Te6edf3localConfigFlow

T8b949e/** Clears the [LocalConfig] data in the data store. */
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffclearLocalConfigTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3localConfigDataSourceTb4b4b4.Te6edf3clearLocalConfigTb4b4b4(Tb4b4b4)
Tb4b4b4}

T8b949e/**
* Updates [LocalConfig] from each [Config] oneOf.
*
* @param config The [Config] to be set.
*/
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffsetLocalConfigTb4b4b4(Te6edf3configTb4b4b4: Te6edf3ConfigTb4b4b4) Tb4b4b4{
Te6edf3localConfigDataSourceTb4b4b4.Te6edf3setLocalConfigTb4b4b4(Te6edf3configTb4b4b4)
Te6edf3configTb4b4b4.Te6edf3loraTff7b72?.Te6edf3let Tb4b4b4{ Te6edf3channelSetDataSourceTb4b4b4.Te6edf3setLoraConfigTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4}
Tb4b4b4}

T8b949e/** Flow representing the [LocalModuleConfig] data store. */
Tff7b72override Tff7b72val Te6edf3moduleConfigFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3LocalModuleConfigTff7b72> Tff7b72= Te6edf3moduleConfigDataSourceTb4b4b4.Te6edf3moduleConfigFlow

T8b949e/** Clears the [LocalModuleConfig] data in the data store. */
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffclearLocalModuleConfigTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3moduleConfigDataSourceTb4b4b4.Te6edf3clearLocalModuleConfigTb4b4b4(Tb4b4b4)
Tb4b4b4}

T8b949e/**
* Updates [LocalModuleConfig] from each [ModuleConfig] oneOf.
*
* @param config The [ModuleConfig] to be set.
*/
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffsetLocalModuleConfigTb4b4b4(Te6edf3configTb4b4b4: Te6edf3ModuleConfigTb4b4b4) Tb4b4b4{
Te6edf3moduleConfigDataSourceTb4b4b4.Te6edf3setLocalModuleConfigTb4b4b4(Te6edf3configTb4b4b4)
Tb4b4b4}

T8b949e// DeviceUIConfig is session-scoped data received fresh in every handshake — no persistence needed.
Tff7b72private Tff7b72val Te6edf3_deviceUIConfigFlow Tff7b72= Te6edf3MutableStateFlowTff7b72<Te6edf3DeviceUIConfig?Tff7b72>Tb4b4b4(Tff7b72nullTb4b4b4)
Tff7b72override Tff7b72val Te6edf3deviceUIConfigFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3DeviceUIConfig?Tff7b72> Tff7b72= Te6edf3_deviceUIConfigFlowTb4b4b4.Te6edf3asStateFlowTb4b4b4(Tb4b4b4)

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffsetDeviceUIConfigTb4b4b4(Te6edf3configTb4b4b4: Te6edf3DeviceUIConfigTb4b4b4) Tb4b4b4{
Te6edf3_deviceUIConfigFlowTb4b4b4.Te6edf3value Tff7b72= Te6edf3config
Tb4b4b4}

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffclearDeviceUIConfigTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3_deviceUIConfigFlowTb4b4b4.Te6edf3value Tff7b72= Tff7b72null
Tb4b4b4}

T8b949e// FileInfo manifest is session-scoped: accumulated during STATE_SEND_FILEMANIFEST, cleared on each new handshake.
Tff7b72private Tff7b72val Te6edf3_fileManifestFlow Tff7b72= Te6edf3MutableStateFlowTff7b72<Te6edf3ListTff7b72<Te6edf3FileInfoTff7b72>Tff7b72>Tb4b4b4(Te6edf3emptyListTb4b4b4(Tb4b4b4)Tb4b4b4)
Tff7b72override Tff7b72val Te6edf3fileManifestFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3ListTff7b72<Te6edf3FileInfoTff7b72>Tff7b72> Tff7b72= Te6edf3_fileManifestFlowTb4b4b4.Te6edf3asStateFlowTb4b4b4(Tb4b4b4)

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffaddFileInfoTb4b4b4(Te6edf3infoTb4b4b4: Te6edf3FileInfoTb4b4b4) Tb4b4b4{
T8b949e// A real device's manifest is bounded by its flash storage (realistically a handful to a few
T8b949e// hundred entries). A rogue/adversarial peer, however, can emit FileInfo packets in a tight loop
T8b949e// for the lifetime of a session -- clearFileManifest only runs on the *next* handshake, so nothing
T8b949e// else bounds this accumulator in between. Cap it defensively, same pattern as the early-packet
T8b949e// buffer in MeshMessageProcessorImpl.
T8b949e//
T8b949e// handleFileInfo dispatches each packet on its own scope.handledLaunch, so addFileInfo calls can
T8b949e// run concurrently -- a plain read-then-write would race (lost updates, or two callers both seeing
T8b949e// size-1 and bypassing the cap). update{} makes the read/cap/append atomic (CAS retry loop).
Tff7b72var Te6edf3capped Tff7b72= Tff7b72false
Te6edf3_fileManifestFlowTb4b4b4.Te6edf3update Tb4b4b4{ Te6edf3current Tff7b72-Tff7b72>
Tff7b72if Tb4b4b4(Te6edf3currentTb4b4b4.Te6edf3size Tff7b72>Tff7b72= Te6edf3MAX_FILE_MANIFEST_ENTRIESTb4b4b4) Tb4b4b4{
Te6edf3capped Tff7b72= Tff7b72true
Te6edf3current
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3capped Tff7b72= Tff7b72false
Te6edf3current Tff7b72+ Te6edf3info
Tb4b4b4}
Tb4b4b4}
Tff7b72if Tb4b4b4(Te6edf3cappedTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3w Tb4b4b4{ Ta5d6ff"Ta5d6ffFile manifest capped at Tffd700$Te6edf3MAX_FILE_MANIFEST_ENTRIESTa5d6ff entries, dropping further FileInfoTa5d6ff" Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffclearFileManifestTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3_fileManifestFlowTb4b4b4.Te6edf3value Tff7b72= Te6edf3emptyListTb4b4b4(Tb4b4b4)
Tb4b4b4}

T8b949e// Region→preset compatibility map is session-scoped: delivered once per handshake, cleared on each new handshake.
Tff7b72private Tff7b72val Te6edf3_loraRegionPresetMapFlow Tff7b72= Te6edf3MutableStateFlowTff7b72<Te6edf3LoRaRegionPresetMap?Tff7b72>Tb4b4b4(Tff7b72nullTb4b4b4)
Tff7b72override Tff7b72val Te6edf3loraRegionPresetMapFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3LoRaRegionPresetMap?Tff7b72> Tff7b72= Te6edf3_loraRegionPresetMapFlowTb4b4b4.Te6edf3asStateFlowTb4b4b4(Tb4b4b4)

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffsetLoraRegionPresetMapTb4b4b4(Te6edf3mapTb4b4b4: Te6edf3LoRaRegionPresetMapTb4b4b4) Tb4b4b4{
Te6edf3_loraRegionPresetMapFlowTb4b4b4.Te6edf3value Tff7b72= Te6edf3map
Tb4b4b4}

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffclearLoraRegionPresetMapTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3_loraRegionPresetMapFlowTb4b4b4.Te6edf3value Tff7b72= Tff7b72null
Tb4b4b4}

T8b949e/** Flow representing the combined [DeviceProfile] protobuf. */
Tff7b72override Tff7b72val Te6edf3deviceProfileFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3DeviceProfileTff7b72> Tff7b72=
Te6edf3combineTb4b4b4(Te6edf3nodeDBTb4b4b4.Te6edf3ourNodeInfoTb4b4b4, Te6edf3channelSetFlowTb4b4b4, Te6edf3localConfigFlowTb4b4b4, Te6edf3moduleConfigFlowTb4b4b4) Tb4b4b4{
Te6edf3nodeTb4b4b4,
Te6edf3channelsTb4b4b4,
Te6edf3localConfigTb4b4b4,
Te6edf3localModuleConfigTb4b4b4,
Tff7b72-Tff7b72>
Te6edf3DeviceProfileTb4b4b4(
Te6edf3long_name Tff7b72= Te6edf3nodeTff7b72?.Te6edf3userTff7b72?.Te6edf3long_nameTb4b4b4,
Te6edf3short_name Tff7b72= Te6edf3nodeTff7b72?.Te6edf3userTff7b72?.Te6edf3short_nameTb4b4b4,
Te6edf3channel_url Tff7b72= Te6edf3channelsTb4b4b4.Te6edf3getChannelUrlTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3toStringTb4b4b4(Tb4b4b4)Tb4b4b4,
Te6edf3config Tff7b72= Te6edf3localConfigTb4b4b4,
Te6edf3module_config Tff7b72= Te6edf3localModuleConfigTb4b4b4,
Te6edf3is_unmessagable Tff7b72= Te6edf3nodeTff7b72?.Te6edf3userTff7b72?.Te6edf3is_unmessagableTb4b4b4,
Te6edf3is_licensed Tff7b72= Te6edf3nodeTff7b72?.Te6edf3userTff7b72?.Te6edf3is_licensedTb4b4b4,
Te6edf3fixed_position Tff7b72=
Tff7b72if Tb4b4b4(Te6edf3node Tff7b72!Tff7b72= Tff7b72null Tff7b72&Tff7b72& Te6edf3localConfigTb4b4b4.Te6edf3positionTff7b72?.Te6edf3fixed_position Tff7b72=Tff7b72= Tff7b72trueTb4b4b4) Tb4b4b4{
Te6edf3nodeTb4b4b4.Te6edf3position
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72null
Tb4b4b4}Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.4.2 - Generated in 0.07s